package com.xxxy.jsq.action.department;

import com.alibaba.fastjson2.JSONObject;
import com.xxxy.jsq.dao.DepartmentDao;
import com.xxxy.jsq.model.Department;
import com.xxxy.jsq.model.Page;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class getAllDepartmentsPageServlet
 */
@WebServlet("/getAllDepartmentsPageServlet")
public class getAllDepartmentsPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getAllDepartmentsPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Department model=new Department();
		DepartmentDao dao=new DepartmentDao();
		String cflag=request.getParameter("cflag");
		String cname=request.getParameter("cname");
		String pid=request.getParameter("pnames");
		if(cflag!=null&&!cflag.equals("")){
			model.setDepartmentFlag(cflag);
		}
		if(cname!=null&&!cname.equals("")){
			model.setDepartmentName(cname);
		}
		if(pid!=null&&!pid.equals("")){
			model.setDepartmentPid(pid);
		}
		String cpage=request.getParameter("page");
		String limit=request.getParameter("limit");
		Page page=new Page();
		if(cpage!=null&&!cpage.equals("")){
			page.setCurrentPage(Integer.parseInt(cpage));
			page.setCount(Integer.parseInt(limit));
		}
		List<Department> list=dao.getAllDataByPage(model, page);
		int total=dao.getCount(model);//获取记录数
		System.out.println(list);
		JSONObject jo=new JSONObject();
		jo.put("code", 0);
		jo.put("msg", "你好");
		jo.put("count", total);
		jo.put("data", list);
		System.out.println(jo.toJSONString());
		PrintWriter out=response.getWriter();
		out.print(jo.toString());
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
